﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using LibrarySolution.Model.基础;
using LibrarySolution.DB;

namespace LibrarySolution.DAO.基础
{
    class KfDao
    {
        String sql;
        List<SqlParameter> list;

        public void insertKf(KfxxDetail kfxxEntity)
        {
            Double money;
            addParas(kfxxEntity);
            sql = "insert into jc_kfxx (bk_no,yh_no,kf_kcfy,kf_glyname,kf_kfsj) " +
                "values(@bkNo,@yhNo,@kfKcfy,@kfGlyname,@kfKfrq)";
            DBExecuteUtil.querySqlV(sql, list.ToArray());

            money = Convert.ToDouble(kfxxEntity.yh_zhye) - Convert.ToDouble(kfxxEntity.kf_kcfy);
            list[3].SqlValue = money;
            sql = "update jc_yhxx set yh_zhye = @yhZhye where yh_no=@yhNo";
            DBExecuteUtil.querySqlV(sql, list.ToArray());
        }

        public void cqkf(KfxxDetail kfxxEntity)
        {
            addParas(kfxxEntity);
            sql = "update jc_jsjl set js_sfkf = '1' where js_yhno=@yhNo and " +
                "js_tsbm=@bkNo and js_ghrq = @kfGhrq";
            DBExecuteUtil.querySqlV(sql, list.ToArray());
        }

        public void dskf(KfxxDetail kfxxEntity)
        {
            addParas(kfxxEntity);
            sql = "update jc_jsjl set js_dskf = '1' where js_yhno=@yhNo and js_tsbm=@bkNo";
            DBExecuteUtil.querySqlV(sql, list.ToArray());
        }

        public void updateRe(KfxxDetail kfxxDetail)
        {
            Double money;
            addParas(kfxxDetail);
       
            money = getYhye(kfxxDetail) - Convert.ToDouble(kfxxDetail.kf_kcfy);
            list[3].SqlValue = money;
            sql = "update jc_yhxx set yh_zhye = @yhZhye where yh_no=@yhNo";
            DBExecuteUtil.querySqlV(sql, list.ToArray());
        }

        public Double getYhye(KfxxDetail kfxxDetail)
        {
            addParas(kfxxDetail);
            Double money;
            sql = "select yh_zhye from jc_yhxx where yh_no = @yhNo";
            money = Convert.ToDouble(DBExecuteUtil.querySql(sql, list.ToArray()));
            return money;
        }

        private void addParas(KfxxDetail kfxxDetail)
        {
            list = new List<SqlParameter>();
            if (kfxxDetail.kf_code != null)
                list.Add(new SqlParameter("@kfCode", kfxxDetail.kf_code));
            if (kfxxDetail.bk_no != null)
                list.Add(new SqlParameter("@bkNo", kfxxDetail.bk_no));
            if (kfxxDetail.yh_no != null)
                list.Add(new SqlParameter("@yhNo", kfxxDetail.yh_no));
            if (kfxxDetail.kf_kcfy != null)
                list.Add(new SqlParameter("@kfKcfy", kfxxDetail.kf_kcfy));
            if (kfxxDetail.yh_zhye != null)
                list.Add(new SqlParameter("@yhZhye", kfxxDetail.yh_zhye));
            if (kfxxDetail.kf_glyname != null)
                list.Add(new SqlParameter("@kfGlyname", kfxxDetail.kf_glyname));
            if (kfxxDetail.kf_kfsj != null)
                list.Add(new SqlParameter("@kfKfrq", kfxxDetail.kf_kfsj));
            if (kfxxDetail.kf_ghrq != null)
                list.Add(new SqlParameter("@kfGhrq", kfxxDetail.kf_ghrq));
        }
    }
}
